package sg.edu.nus.iss.ooad.jass.dao.file;

import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IJobseekerDAO;
import sg.edu.nus.iss.ooad.jass.entity.IJASSEntity;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;

/**
 *
 * Jobseeker Data Access Object for Properties File implementation.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : JobseekerDAOPropertiesImpl.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class JobseekerDAOPropertiesImpl extends AbstractPropertiesDAO implements IJobseekerDAO {

        //Jobseekers List
        public static Hashtable<String, IJASSEntity> jobseekerCache = new Hashtable<String, IJASSEntity>();
        //Logger
        private final static Logger logger = Logger.getLogger(JobseekerDAOPropertiesImpl.class.getName());
        //Properties filename
        public static final String JOBSEEKER_FILE = "data.jobseeker.properties";
        //Variable to hold Properties for Jobseeker
        public static Properties properties = null;

        //Load at start-up
        static {
                logger.info("Start loading " + JOBSEEKER_FILE);
                properties = loadProperties(JOBSEEKER_FILE);
                if (properties != null) {
                        Jobseeker jobseeker = new Jobseeker();
                        jobseekerCache = jobseeker.fromJASSPropertiesFormat(properties);
                }
                logger.info("Finish loading " + JOBSEEKER_FILE);
        }

        /**
         * Constructor
         */
        public JobseekerDAOPropertiesImpl() {
        }

        /**
         * Create new Jobseeker entity and store it to the persistent data.
         * @param jobseeker
         * @return
         */
        public synchronized boolean createJobseeker(Jobseeker jobseeker) {
                boolean retValue = false;

                //get number to total record
                int iTotalRecord = 0;
                String sTotalRecord = properties.getProperty("NumOfItems");
                if (sTotalRecord != null) {
                        iTotalRecord = Integer.parseInt(sTotalRecord);
                }
                iTotalRecord = iTotalRecord + 1;
                jobseeker.setID(String.valueOf(iTotalRecord));

                //update properties
                //JOBSEEKER_ATTRIBUTES = {"ID", "Name", "Email", "Password", "Address", "Resume", "Deleted"};
                properties.put("ID" + String.valueOf(iTotalRecord), jobseeker.getID());
                properties.put("Name" + String.valueOf(iTotalRecord), jobseeker.getName());
                properties.put("Email" + String.valueOf(iTotalRecord), jobseeker.getEmail());
                properties.put("Password" + String.valueOf(iTotalRecord), jobseeker.getPassword());
                properties.put("Address" + String.valueOf(iTotalRecord), jobseeker.getAddress());
                properties.put("Resume" + String.valueOf(iTotalRecord), jobseeker.getResume());
                properties.put("Deleted" + String.valueOf(iTotalRecord), jobseeker.getDeleted());
                properties.setProperty("NumOfItems", String.valueOf(iTotalRecord));

                //save it and update the cache
                retValue =storeProperties(properties, JOBSEEKER_FILE);
                jobseekerCache.put(jobseeker.getID(), jobseeker);

                return retValue;
        }

        /**
         * Retrieve an Jobseeker from persistent data.
         * @param ID
         * @return
         */
        public Jobseeker retrieveJobseeker(String ID) {
                Jobseeker retValue = null;

                retValue = (Jobseeker) jobseekerCache.get(ID);

                return retValue;
        }

        /**
         *
         * @param jobseeker
         * @return
         */
        public synchronized boolean updateJobseeker(Jobseeker jobseeker) {
                boolean retValue = false;

                if (jobseekerCache.containsKey(jobseeker.getID())) {
                        //update properties
                        //JOBSEEKER_ATTRIBUTES = {"ID", "Name", "Email", "Password", "Address", "Resume", "Deleted"};
                        properties.setProperty("Name" + jobseeker.getID(), jobseeker.getName());
                        properties.setProperty("Email" + jobseeker.getID(), jobseeker.getEmail());
                        properties.setProperty("Password" + jobseeker.getID(), jobseeker.getPassword());
                        properties.setProperty("Address" + jobseeker.getID(), jobseeker.getAddress());
                        properties.setProperty("Resume" + jobseeker.getID(), jobseeker.getResume());
                        properties.setProperty("Deleted" + jobseeker.getID(), jobseeker.getDeleted());

                        //save it and update the cache
                        storeProperties(properties, JOBSEEKER_FILE);
                        jobseekerCache.remove(jobseeker.getID());
                        jobseekerCache.put(jobseeker.getID(), jobseeker);

                        retValue = true;
                }

                return retValue;
        }

        /**
         *
         * @param jobseeker
         * @return
         */
        public boolean deleteJobseeker(Jobseeker jobseeker) {
                throw new UnsupportedOperationException("Not supported yet.");
        }

        /**
         *
         * @return
         */
        public Jobseeker getJobseekerByEmail(String email) {
                Jobseeker retValue = new Jobseeker();

                //Looping the whole records from cache
                Enumeration<IJASSEntity> elements = jobseekerCache.elements();
                while (elements.hasMoreElements()) {
                        Jobseeker jobseeker = (Jobseeker) elements.nextElement();
                        if (email.matches(jobseeker.getEmail())) {
                                retValue = jobseeker;
                                break;
                        }
                }

                return retValue;
        }

        /**
         *
         * @return
         */
        public List<Jobseeker> getAllJobseeker() {
                List<Jobseeker> retValue = new ArrayList<Jobseeker>();

                //Looping the whole records from cache
                Enumeration<IJASSEntity> elements = jobseekerCache.elements();
                while (elements.hasMoreElements()) {
                        Jobseeker jobseeker = (Jobseeker) elements.nextElement();
                        retValue.add(jobseeker);
                }

                return retValue;
        }

        /**
         *
         * @return
         */
        public static synchronized Hashtable<String, IJASSEntity> cache() {
                return jobseekerCache;
        }
}
